package com.hifinan.log;

import java.io.*;

public class LogTestDriver{
	public static boolean append = true;
	public static void main(String[] args){
		LogTestDriver a = new LogTestDriver();
		a.record("sudoku.log", "hello~");
		a.record("sudoku.log", "world~");
	}
	public static boolean record(String path, String message){
		boolean success = false;
		File file = new File(path);
		if(!file.exists()){
			try{
				file.createNewFile();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(file.isFile()){
			try{
				FileWriter fw = new FileWriter(file, append);
				BufferedWriter bw = new BufferedWriter(fw);
				bw.write(message);
				bw.newLine();
				bw.flush();
				bw.close();
				success = true;
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			System.out.println("please input a correct file name");
		}
		return success;
	}
	public static boolean save(String path, Object o){
		boolean success = false;
		append = false;
		File file = new File(path);
		if(!file.exists()){
			try{
				file.createNewFile();
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		if(file.isFile()){
			try{
				FileOutputStream fos = new FileOutputStream(file, append);
				ObjectOutputStream oos = new ObjectOutputStream(fos);
				System.out.println(o.toString());
				oos.writeObject(o);
				oos.close();
				success = true;
			}catch(Exception e){
				e.printStackTrace();
			}
		}else{
			System.out.println("please input a correct file name");
		}
		System.out.println("in  LogTestDriver.save() method.");
		return success;
	}
}
